package promsvc

import (
	"errors"
	"fmt"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver"
	v1 "gluenet/pkg/log/v1"

	"github.com/spf13/viper"
)

func init() {
	depend.Provide(NewPromConfig)
	depend.Provide(NewService)
}

type Service struct {
	logger  v1.Logger
	promCfg *PromConfig
}

func NewService(promCfg *PromConfig) *Service {
	return &Service{
		logger:  v1.NewLogger(""),
		promCfg: promCfg,
	}
}

const (
	keyPromAddr = driver.KeyDriverPerfix + `prom.addr`
)

type PromConfig struct {
	Api      string
	ApiRange string
}

func NewPromConfig(vp *viper.Viper) (*PromConfig, error) {
	addr := vp.GetString(keyPromAddr)
	if addr == "" {
		return nil, errors.New("empty prometheus addr")
	}

	return &PromConfig{
		Api:      fmt.Sprintf("http://%s/api/v1/query", addr),
		ApiRange: fmt.Sprintf("http://%s/api/v1/query_range", addr),
	}, nil
}
